package cn.sunshine.org.controller;

import cn.sunshine.basic.annotation.PreAuthorize;
import cn.sunshine.basic.exception.BusinessException;
import cn.sunshine.basic.utils.AjaxResult;
import cn.sunshine.basic.utils.ExcelUtil;
import cn.sunshine.basic.utils.PageList;
import cn.sunshine.org.domain.Shop;
import cn.sunshine.org.domain.ShopAuditLog;
import cn.sunshine.org.query.ShopQuery;
import cn.sunshine.org.service.IShopService;
import org.apache.velocity.runtime.directive.Foreach;
import org.apache.velocity.runtime.directive.contrib.For;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "新增或者修改店铺" ,sn = "shop:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @PreAuthorize(name = "删除店铺" ,sn = "shop:remove")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据Id查询" ,sn = "shop:get")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    @PreAuthorize(name = "查询所有" ,sn = "shop:loadAll")
    public List<Shop> list() {

        return shopService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize(name = "高级查询查询所有" ,sn = "shop:queryData")
    public PageList<Shop> json(@RequestBody ShopQuery query) {

        return shopService.queryData(query);
    }

    /**
     * @Title: settlement
     * @Description: 店铺入驻接口
     * @Author: Mr.She
     * @Version: 1.0
     * @Date: 2022/7/1 14:08
     * @Parameters: [department]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */
    @PostMapping("/settlement") // 入驻交接口
    @PreAuthorize(name = "商家入驻" ,sn = "shop:settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
     * @Title: reject
     * @Description: 驳回店铺
     * @Author: Mr.She
     * @Version: 1.0
     * @Date: 2022/7/2 10:30
     * @Parameters: [shopAuditLog]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */
    @PostMapping("/audit/reject")
    @PreAuthorize(name = "驳回入驻" ,sn = "shop:reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

/*
* @Title: pass
* @Description: 通过店铺
* @Author: Mr.Jiang
* @Version: 1.0
* @Date:  2022/7/3 22:41
* @Parameters: [shopAuditLog]
* @Return cn.sunshine.basic.utils.AjaxResult
*/
    @PostMapping("/audit/pass")
    @PreAuthorize(name = "通过入驻" ,sn = "shop:pass")

    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    /*
    * @Title: refuse
    * @Description: 拒绝
    * @Author: Mr.Jiang
    * @Version: 1.0
    * @Date:  2022/7/3 22:50
    * @Parameters: [shopAuditLog]
    * @Return cn.sunshine.basic.utils.AjaxResult
    */
    @PostMapping("/audit/refuse")
    @PreAuthorize(name = "拒绝入驻" ,sn = "shop:refuse")

    public AjaxResult refuse(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.refuse(shopAuditLog);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    /*
     * @Title: exportExcel
     * @Description: 导出店铺信息
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/3 11:13
     * @Parameters: [response]
     * @Return void
     */
    @GetMapping("/export/excel")
    @PreAuthorize(name = "导出店铺信息" ,sn = "shop:exportExcel")
    public void exportExcel(HttpServletResponse response) {
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息表.xlsx", response);
    }

    /*
     * @Title: importExcel
     * @Description: excel导入
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/3 11:13
     * @Parameters: [file]
     * @Return void
     */
    @PostMapping("/import/excel")
    @PreAuthorize(name = "导入店铺信息" ,sn = "shop:importExcel")
    public void importExcel(@RequestPart(value = "file", required = true) MultipartFile file) {
        // 1.使用EasyPoi帮你解析Excel，解析成Java对象
        // MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        // 2.做批量添加，自行实现
       shops.forEach(System.out::println);
        shopService.batchAdd(shops);

    }

    /*
     * @Title: echarts
     * @Description: 店铺报表数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/3 11:14
     * @Parameters: []
     * @Return java.util.Map<java.lang.String,java.lang.Object>
     */
    @GetMapping("/echarts")
    public Map<String, Object> echarts() {
        return shopService.loadEcharts();
    }

}
